#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QStyle>
#include <QDebug>

class TabButtonTest : public QWidget
{
    Q_OBJECT

public:
    explicit TabButtonTest(QWidget *parent = nullptr) : QWidget(parent)
    {
        setWindowTitle("Tab按钮选中状态测试");
        setMinimumSize(400, 200);
        
        QVBoxLayout *layout = new QVBoxLayout(this);
        
        // 创建四个Tab按钮
        realtimeBtn = new QPushButton("📈 实时监控", this);
        sourceBtn = new QPushButton("📊 收入来源", this);
        trendBtn = new QPushButton("📉 趋势分析", this);
        forecastBtn = new QPushButton("🔮 收入预测", this);
        
        // 设置初始选中状态
        setActiveTabButton(realtimeBtn);
        
        // 连接点击信号
        connect(realtimeBtn, &QPushButton::clicked, [this]() { setActiveTabButton(realtimeBtn); });
        connect(sourceBtn, &QPushButton::clicked, [this]() { setActiveTabButton(sourceBtn); });
        connect(trendBtn, &QPushButton::clicked, [this]() { setActiveTabButton(trendBtn); });
        connect(forecastBtn, &QPushButton::clicked, [this]() { setActiveTabButton(forecastBtn); });
        
        // 添加到布局
        layout->addWidget(realtimeBtn);
        layout->addWidget(sourceBtn);
        layout->addWidget(trendBtn);
        layout->addWidget(forecastBtn);
    }

private:
    QPushButton *realtimeBtn;
    QPushButton *sourceBtn;
    QPushButton *trendBtn;
    QPushButton *forecastBtn;
    
    void setActiveTabButton(QPushButton *activeBtn)
    {
        // 定义选中状态的样式
        const QString activeStyle = 
            "background-color: #1a73e8;"
            "color: white;"
            "border: none;"
            "border-radius: 4px;"
            "padding: 6px 12px;"
            "margin: 2px;"
            "font-size: 12px;";
        
        // 定义未选中状态的样式
        const QString inactiveStyle = 
            "background-color: #f1f3f4;"
            "color: #5f6368;"
            "border: none;"
            "border-radius: 4px;"
            "padding: 6px 12px;"
            "margin: 2px;"
            "font-size: 12px;";
        
        // 所有Tab按钮列表
        QList<QPushButton*> allTabButtons = {
            realtimeBtn, sourceBtn, trendBtn, forecastBtn
        };
        
        // 重置所有按钮为未选中状态
        for (QPushButton *btn : allTabButtons) {
            btn->setStyleSheet(inactiveStyle);
            // 强制刷新样式
            btn->style()->unpolish(btn);
            btn->style()->polish(btn);
            btn->update();
        }
        
        // 设置被点击的按钮为选中状态
        if (activeBtn) {
            activeBtn->setStyleSheet(activeStyle);
            // 强制刷新样式
            activeBtn->style()->unpolish(activeBtn);
            activeBtn->style()->polish(activeBtn);
            activeBtn->update();
            
            qDebug() << "设置选中按钮:" << activeBtn->text();
        }
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    TabButtonTest window;
    window.show();
    
    return app.exec();
}

#include "tab_test.moc"